package com.plian.system.controller.sys.orgzation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.sys.BaseServiceOrg;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.orgzation.IBaseServiceOrgService;
import com.plian.system.service.sys.orgzation.IServiceOrgFormTypeService;
import com.plian.system.vo.sys.BaseServiceOrgVO;
import com.plian.system.vo.sys.ServiceOrgFormTypeVO;
import com.plian.system.wrapper.sys.BaseServiceOrgWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping(value = "api/baseserviceorg")
@Api(value = "业务组织主表", tags = "业务组织主表接口")
public class BaseServiceOrgController {

    private IBaseServiceOrgService baseServiceOrgService;

    private IServiceOrgFormTypeService serviceOrgFormTypeService;

    private BaseServiceOrgWrapper wrapper;

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入id")
    public R<BaseServiceOrgVO> detail(@RequestBody BaseServiceOrg baseServiceOrg){
        BaseServiceOrg one = baseServiceOrgService.getOne(Condition.getQueryWrapper(baseServiceOrg));
        BaseServiceOrgVO vo = wrapper.entityToVO(one);
        vo.setCheckedName(serviceOrgFormTypeService.getCheckName(vo.getId()));
        return R.data(vo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<BaseServiceOrgVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        QueryWrapper<BaseServiceOrg> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new BaseServiceOrg()), entityList);
        IPage<BaseServiceOrg> pages = baseServiceOrgService.page(Condition.getPage(query), queryWrapper);
        List<BaseServiceOrgVO> baseFormListVOS = wrapper.entityToVO(pages.getRecords());
        return ListResult.data(pages, baseFormListVOS);
    }

    @Resubmit(delaySeconds = 0)
    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入baseServiceOrg")
    @LogOperation("业务组织-提交")
    public R submit(@RequestBody BaseServiceOrg baseServiceOrg){
        return R.status(baseServiceOrgService.submit(baseServiceOrg));
    }

    @PostMapping("/remove")
    @ApiOperation(value = "批量删除", notes = "传入ids")
    @LogOperation("业务组织-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return R.status(baseServiceOrgService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
    }

    @GetMapping("{id}/form-type-tree")
    @ApiOperation(value = "获得模块树", notes = "传入ids")
    public R<List<ServiceOrgFormTypeVO>> formTypeTree(@PathVariable Long id) {
        return R.data(serviceOrgFormTypeService.formTypeTree(id));
    }

//    @PostMapping("/check-form-type")TODO DEL
//    @ApiOperation(value = "校验 所选业务", notes = "传入baseServiceOrgVO")
//    public R<String> checkFormType(@RequestBody BaseServiceOrgVO baseServiceOrgVO){
//        return R.data(serviceOrgFormTypeService.checkList(baseServiceOrgVO));
//    }

    @Resubmit(delaySeconds = 0)
    @PostMapping("/submit-form-type")
    @ApiOperation(value = "保存 配置业务", notes = "传入baseServiceOrgVO")
    @LogOperation("业务组织-保存配置业务")
    public R submitFormType(@RequestBody BaseServiceOrgVO baseServiceOrgVO){
        return R.status(serviceOrgFormTypeService.saveList(baseServiceOrgVO));
    }


    @GetMapping("/init-org")
    @ApiOperation(value = "主版本组织初始化")
    @LogOperation("业务组织-主版本组织初始化")
    public R initOrg(){
        return R.status(baseServiceOrgService.initOrg());
    }
}
